package com.sc.userspringboot.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.userspringboot.pojo.*;
import com.sc.userspringboot.service.*;
import com.sc.userspringboot.utils.JwtUtils;
import com.sc.userspringboot.dto.PostPage;
import com.sc.userspringboot.utils.RedisUtils;
import com.sc.userspringboot.utils.ResponseMsg;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home")
public class HYJ_Home {
    @Autowired
    private IUsersService iUsersService;//用户
    @Autowired
    private IPostsService iPostsService;//帖子
    @Autowired
    private IGamesService iGamesService;//游戏
    @Autowired
    private IPostTagsService iPostTagsService;//帖子标签
    @Autowired
    private IPostImagesService iPostImagesService;//帖子图片
    @Autowired
    private IPostCollectionsService iPostCollectionsService;//帖子收藏
    @Autowired
    private ICommentsService iCommentsService;//帖子评论
    @Autowired
    private IFollowsService iFollowsService;//关注

    @Autowired
    private RedisUtils redisUtils;
    //拉取推荐的数据
    @RequestMapping("pageQuery")
    public ResponseMsg pageQuery(@RequestBody PostPage postPage,
                                 HttpServletRequest request){
        List<Posts> randomPosts = null;

        try {
            String token=request.getHeader("token");
            Claims claims= JwtUtils.parseJWT(token);
            Users user = JSON.parseObject(claims.getSubject(), Users.class);
            randomPosts= iPostsService.selectRandomPosts(postPage.getLimit());
            for (Posts posts : randomPosts) {
                posts.setUser(iUsersService.getById(posts.getUserId()));
                posts.setPostImages(iPostImagesService.getByPostId(posts.getPostId()));
                posts.setGames(iGamesService.getById(posts.getGameId()));
                QueryWrapper<Comments> wrapper =new QueryWrapper<>();
                wrapper.eq("post_id",posts.getPostId());
                posts.setCommentCount(iCommentsService.list(wrapper).size());
            }

        } catch (Exception e) {

        }

        return ResponseMsg.getInstance(200, "数据取成功", randomPosts);

    }


    //得到前六个游戏
    @RequestMapping("getSixGames")
    public ResponseMsg getSixGames(){
        IPage<Games> page = new Page<>(1, 6);
        List<Games> games= iGamesService.list(page);
        return  ResponseMsg.getInstance(200,"数据取成功",games);
    }



    //获取我关注的用户
    // getMyFollowUser 方法可以优化为：
    @RequestMapping("getMyFollowUser")
    public ResponseMsg getMyFollowUser(@RequestBody PostPage postPage, HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            Claims claims = JwtUtils.parseJWT(token);
            Users user = JSON.parseObject(claims.getSubject(), Users.class);

            IPage<Follows> page = new Page<>(postPage.getPage(), postPage.getLimit());
            QueryWrapper<Follows> wrapper = new QueryWrapper<>();
            wrapper.eq("follower_id", user.getUserId());
            List<Follows> follows = iFollowsService.list(page, wrapper);

            if (follows == null || follows.size() == 0) {
                return ResponseMsg.getInstance(200, "没有关注用户", new ArrayList<>());
            }

            List<Users> users = new ArrayList<>();
            for (Follows follow : follows) {
                Users one = iUsersService.getById(follow.getFolloweeId());
                users.add(one);
            }

            return ResponseMsg.getInstance(200, "数据取成功", users);
        } catch (Exception e) {
            return ResponseMsg.getInstance(400, "未登录或登录失效");
        }
    }
    //获取关注的推荐为
    @RequestMapping("getFollow")
    public ResponseMsg getFollow(@RequestBody PostPage postPage,HttpServletRequest request) {
        List<Posts> randomPosts;

        try {
            String token=request.getHeader("token");
            Claims claims= JwtUtils.parseJWT(token);
            Users user = JSON.parseObject(claims.getSubject(), Users.class);
            randomPosts=null;//暂时不写

        } catch (Exception e) {
            System.out.println("未登录,数据无偏好");

            // 调用服务层获取随机帖子
            randomPosts= iPostsService.selectRandomPosts(postPage.getLimit());
            for (Posts posts : randomPosts) {
                posts.setUser(iUsersService.getById(posts.getUserId()));
                posts.setPostImages(iPostImagesService.getByPostId(posts.getPostId()));
                posts.setGames(iGamesService.getById(posts.getGameId()));
                QueryWrapper<Comments> wrapper =new QueryWrapper<>();
                wrapper.eq("post_id",posts.getPostId());
                posts.setCommentCount(iCommentsService.list(wrapper).size());
            }
        }

        return ResponseMsg.getInstance(200, "数据取成功", randomPosts);
    }


}
